package com.iswoak.netctoss.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.iswoak.netctoss.dao.impl.FeeInfoDaoImpl;
import com.iswoak.netctoss.entity.FeeInfo;
import com.iswoak.netctoss.service.FeeInfoService;
import com.iswoak.netctoss.service.impl.FeeInfoServiceImpl;
import com.iswoak.netctoss.util.PageUtil;

import net.sf.json.JSONArray;

/**
 * @version: 1.0
 * @author: fxz
 * @Description:
 * @date: 2019年7月6日 下午2:52:12
 */
@WebServlet("/fee/*")
public class FeeServlet extends HttpServlet {
	FeeInfoService FeeInfoService = new FeeInfoServiceImpl();
	FeeInfoDaoImpl feeInfoDaoImpl = new FeeInfoDaoImpl();
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		String uri = req.getRequestURI();
		if(uri.endsWith("/list")) {//查询页
			list(req,resp);
		} else if(uri.endsWith("/toDetailJsp")) {
			int costId = Integer.valueOf(req.getParameter("costId"));
			FeeInfo feeInfo = feeInfoDaoImpl.findByDetailId(costId);
			req.setAttribute("feeInfo", feeInfo );
			req.getRequestDispatcher("/jsp/fee/fee_detail.jsp").forward(req, resp);
		} else if(uri.endsWith("/toAdd")) {
			req.getRequestDispatcher("/jsp/fee/fee_add.jsp").forward(req, resp);
		} else if(uri.endsWith("/feeAdd")) {
			
		}else if(uri.endsWith("/del")) {
			del(req,resp);
		}else if(uri.endsWith("/updStatus")) {
			updStatus(req,resp);
		}else if(uri.endsWith("/sort")) {
			sort(req,resp);
		} 
	}
	
	//排序
	private void sort(HttpServletRequest req, 
				HttpServletResponse resp) throws ServletException, IOException {
		//资费ID
		Integer currPage = Integer.valueOf(req.getParameter("currPage"));//页数
		Integer pageSize = Integer.valueOf(req.getParameter("pageSize"));//每页条数
		
		String column = req.getParameter("column");//排序字段
		String flag = req.getParameter("flag");//排序规则（升降序）
		
		//查询结果
		List<FeeInfo> list = new ArrayList<>();
		list = FeeInfoService.sortList(column, flag, currPage, pageSize);		
		JSONArray jsonArray = JSONArray.fromObject(list);
		PrintWriter pw = resp.getWriter();
		pw.println(jsonArray.toString());	
		
	}
	
	
	//修改状态
	private void updStatus(HttpServletRequest req, 
			HttpServletResponse resp) throws ServletException, IOException {
		//资费ID
		Integer costId = Integer.valueOf(req.getParameter("costId"));
		int rs = FeeInfoService.updateStatus(costId);//修改状态为开通
		PrintWriter pw = resp.getWriter();
		if(rs > 0) {
			pw.println("OK");	
		} else {
			pw.println("ERROR");	
		}	
	}
	
		
	//删除
	private void del(HttpServletRequest req, 
			HttpServletResponse resp) throws ServletException, IOException {
		Integer costId = Integer.valueOf(req.getParameter("costId"));
		int rs = FeeInfoService.del(costId);//删除
		PrintWriter pw = resp.getWriter();
		if(rs > 0) {
			pw.println("OK");	
		} else {
			pw.println("ERROR");	
		}	
	}
	
	private void list(HttpServletRequest req, 
			HttpServletResponse resp) throws ServletException, IOException {
		Integer currPage = null;//页数
		FeeInfo feeInfo = new FeeInfo();
		if(!StringUtils.isBlank(req.getParameter("currPage"))) {
			currPage = Integer.valueOf(req.getParameter("currPage"));
		} else {
			currPage = 1;
		}
		
		Integer pageSize = null;//每页条数
		if(!StringUtils.isBlank(req.getParameter("pageSize"))) {
			pageSize = Integer.valueOf(req.getParameter("pageSize"));
		} else {
			pageSize = 2;
		}
		
		
		PageUtil<FeeInfo> pageUtil = FeeInfoService.page(currPage, pageSize);
		req.setAttribute("page", pageUtil);
		//模拟查询数据转发到角色查询页
		req.getRequestDispatcher("/jsp/fee/fee_list.jsp").forward(req, resp);
	}

}
